package com.jinxin.platform.cdfaceacs.controller;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdfaceacs.bean.CdfaceacsDevice;
import com.jinxin.platform.cdfaceacs.common.Constant;
import com.jinxin.platform.cdfaceacs.redis.RedisUtil;
import com.jinxin.platform.cdfaceacs.rpc.SysUserRpc;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsDeviceService;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-05-21
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/faceAcsOpenRecord")
@Slf4j
public class FaceAcsOpenRecordController {
    @Autowired
    private IFaceAcsRecordService acsRecordService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysUserRpc sysUserRpc;
    @Autowired
    private IFaceAcsDeviceService acsDeviceService;
    private ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 200, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(5));

    @RequestMapping("/list")
    public Map<String,Object> list(HttpServletRequest request,@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false) String teamId, @RequestParam(required = false) String workTypeId,
                                   @RequestParam(required = false) String name, @RequestParam(required = false) String startDate,
                                   @RequestParam(required = false) String endDate) throws ExecutionException, InterruptedException {
        String key = Constant.CDFACEACS_REDIS_SERVER;
        if(!redisUtil.hasKey(key)){
            String scheme = request.getScheme();
            String serverName = request.getServerName();
            int port = request.getServerPort();
            String serverAddress = scheme + "://" + serverName + ":" + port + "/unAuth/api/openRecordCallBack";
            log.info("服务器地址：" + serverAddress);
            // 设置识别回调
            List<CdfaceacsDevice> deviceList = acsDeviceService.list(new HashMap());
            int count = 0;
            for (CdfaceacsDevice device : deviceList) {
                String password = device.getPassword();
                String serialNum = device.getSerialNum();
                String productCode = device.getProductCode();
                SetRecognitionCallbackTask callbackTask = new SetRecognitionCallbackTask(password, serverAddress, serialNum, productCode);
                FutureTask<JsonResult> task = new FutureTask<>(callbackTask);
                executor.execute(task);
                JsonResult jsonResult = task.get();
                if(jsonResult.getSuccess()){
                    count ++;
                }
            }
            if(count > 0 && count == deviceList.size()){
                redisUtil.set(key,serverAddress);
            }
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("searchKey", name);
        param.put("teamId", teamId);
        param.put("workTypeId", workTypeId);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("excludeStranger", "excludeStranger");
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", acsRecordService.listPageCount(param));
        resultMap.put("data", acsRecordService.listPage(param));
        return resultMap;
    }

    class SetRecognitionCallbackTask implements Callable<JsonResult> {
        private String password;
        private String url;
        private String serialNum;
        private String productCode;

        public SetRecognitionCallbackTask(String password, String url, String serialNum, String productCode) {
            this.password = password;
            this.url = url;
            this.serialNum = serialNum;
            this.productCode = productCode;
        }

        @Override
        public JsonResult call() throws Exception {
            return sysUserRpc.setRecognitionCallback(this.password,this.url,this.serialNum,this.productCode);
        }
    }


}
